package nl.unimaas.games.tron.player;

import java.awt.Color;
import nl.unimaas.games.tron.player.mcts.SelectionStrategy;

import org.w3c.dom.NamedNodeMap;

/**
 * 
 * @author Cliff Laschet
 */
public class LGRPBPlayer extends LGRPBMCTSPlayer {

    private static final long serialVersionUID = 1L;
    public final static double DEFAULT_C = 10;
    public final static int DEFAULT_T = 30;
    public final static int DEFAULT_W = 10;

    public LGRPBPlayer(String name, int num, Color c) {
        super(name, num, c);
        super.select = new SelectionStrategy.NGramPBUCTSelection(this.playout2.getNGram(), DEFAULT_C, DEFAULT_T, DEFAULT_W, syncMoves);
    }

    public static Player fromXml(org.w3c.dom.Node playerRoot, int num, Color color) {
        return fromXml(playerRoot, new LGRPBPlayer(LGRPBPlayer.class.getSimpleName(), num, color));
    }

    public static Player fromXml(org.w3c.dom.Node playerRoot, LGRPBPlayer player) {
        LGRPBMCTSPlayer.fromXml(playerRoot, player);
        NamedNodeMap attrs = playerRoot.getAttributes();
        double C = DEFAULT_C;
        int T = DEFAULT_T;
        int W = DEFAULT_W;
        
        if (attrs.getNamedItem("C") != null) {
            try {
                C = Double.parseDouble(attrs.getNamedItem("C").getNodeValue());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (attrs.getNamedItem("T") != null) {
            try {
                T = Integer.parseInt(attrs.getNamedItem("T").getNodeValue());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        if (attrs.getNamedItem("W") != null) {
            try {
                W = Integer.parseInt(attrs.getNamedItem("W").getNodeValue());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        player.select = new SelectionStrategy.NGramPBUCTSelection(player.playout2.getNGram(), C, T, W, player.syncMoves);

        player.description = String.format("select=%s playout1=%s playout2=%s cut=%s final=%s  expCut=" + player.applyExpansionCutOff + " simCut=" + player.applySimulationCutOff + " C=%.1f T=%d"
                + " alphaRandom = " + player.alphaRandom + "syncLGRMoves: " + player.playoutSyncMoves + " Forgetting: " + player.LGRForgetting+ " ", player.select, player.playout1, player.playout2, player.cutOff, player.finalMove, C, T);
        return player;
    }

    @Override
    public LGRPBPlayer clone() {
        LGRPBPlayer player = new LGRPBPlayer(getName(), getNumber(), getColor());
        player.select = select;
        player.finalMove = finalMove;
        player.cutOff = cutOff;
        player.spaceEval = spaceEval;
        player.playout1 = playout1;
        player.playout2 = playout2;
        player.description = description;
        player.applyExpansionCutOff = applyExpansionCutOff;
        player.applySimulationCutOff = applySimulationCutOff;
        player.applySpaceEstimation = applySpaceEstimation;
        player.playoutSyncMoves = playoutSyncMoves;
        player.LGRForgetting = LGRForgetting;
        player.reqSimulations = reqSimulations;
        return player;
    }
}
